package com.tony.websocket.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: WebSocketConfig
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/05/20:21
 * @Description: 配置类
 */
@Configuration
@EnableWebSocketMessageBroker   //开启websocket消息代理
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
    * @Description: configureMessageBroker 配置消息代理
    * @Param: [registry]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/5 20:25
    */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //前缀为/topic的消息时，就将此消息转发给websocket代理，然后消息代理将消息转发给客户端
        registry.enableSimpleBroker("/topic","/queue");
        // 前缀为/app的消息，就被代理处理
        // 配置一个或多个前缀，通过这些前缀过滤取要被具体的方法处理的消息
        registry.setApplicationDestinationPrefixes("/app");
    }

    /**
    * @Description: registerStompEndpoints 注册连接点
    * @Param: [registry]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/5 20:33
    */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("chat").withSockJS();
    }
}
